package com.hungdev.trangxanh;

import java.util.Calendar;

import com.example.trangxanh.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.SharedPreferences.Editor;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Switch swCall;
	private TextView tv;
	private LinearLayout lnFrom, lnTo;
	private CheckBox cbAlways;
	private static Button btnFrom, btnTo;
	private static boolean flagFrom, flagTo;
	private static int fromHour, fromMin, toHour, toMin;
	private SharedPreferences pref;
	private Editor editor;
	private Spinner spinnerOption;
	private int back = 1;
	private static long backPressed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();

		setPrefer();

		eventView();
	}

	/**
	 * 
	 */
	private void eventView() {
		btnFrom.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				flagFrom = true;
				DialogFragment newFragment = new TimePicker();
				newFragment.show(getFragmentManager(), "timePicker");
			}
		});

		btnTo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				flagTo = true;
				DialogFragment newFragment = new TimePicker();
				newFragment.show(getFragmentManager(), "timePicker");
			}
		});

		cbAlways.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					lnFrom.setVisibility(View.INVISIBLE);
					lnTo.setVisibility(View.INVISIBLE);
				} else {
					lnFrom.setVisibility(View.VISIBLE);
					lnTo.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	/**
	 * 
	 */
	private void setPrefer() {
		pref = getApplicationContext().getSharedPreferences("stateUI", 0);
		editor = pref.edit();

		// dat trang thai cho giao dien tu sharepreference
		swCall.setChecked(pref.getBoolean("switch", false));
		fromHour = pref.getInt("fromHour", 0);
		fromMin = pref.getInt("fromMinute", 0);
		toHour = pref.getInt("toHour", 0);
		toMin = pref.getInt("toMinute", 0);
		String fromH = fromHour < 10 ? "0" + fromHour : String
				.valueOf(fromHour);
		String fromM = fromMin < 10 ? "0" + fromMin : String.valueOf(fromMin);
		String toH = toHour < 10 ? "0" + toHour : String.valueOf(toHour);
		String toM = toMin < 10 ? "0" + toMin : String.valueOf(toMin);
		btnFrom.setText(fromH + ":" + fromM);
		btnTo.setText(toH + ":" + toM);

		if (pref.getBoolean("always", false)) {
			// Toast.makeText(getApplicationContext(), "true",
			// Toast.LENGTH_SHORT).show();
			cbAlways.setChecked(true);
			lnFrom.setVisibility(View.INVISIBLE);
			lnTo.setVisibility(View.INVISIBLE);
		} else {
			// Toast.makeText(getApplicationContext(), "false",
			// Toast.LENGTH_SHORT).show();
			cbAlways.setChecked(false);
			lnFrom.setVisibility(View.VISIBLE);
			lnTo.setVisibility(View.VISIBLE);
		}
	}

	private void initView() {
		swCall = (Switch) findViewById(R.id.sw_call);
		tv = (TextView) findViewById(R.id.tv_set_time);
		btnFrom = (Button) findViewById(R.id.time_block_from);
		btnTo = (Button) findViewById(R.id.time_block_to);
		lnFrom = (LinearLayout) findViewById(R.id.ln_from);
		lnTo = (LinearLayout) findViewById(R.id.ln_to);
		cbAlways = (CheckBox) findViewById(R.id.cb_always);
		//spinnerOption = (Spinner) findViewById(R.id.spinner_option);

		/*String arr[] = { "Tắt ngay", "Chỉ rung", "Im lặng" };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, arr);
		adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		spinnerOption.setAdapter(adapter);*/
	}

	

	// Bắt đầu chạy service
	public void startService() {
		// gui du lieu tu activity cho service
		Intent i = new Intent(this, BlockCallService.class);

		if (!cbAlways.isChecked()) {
			i.putExtra("fromHour", fromHour);
			i.putExtra("fromMinute", fromMin);
			i.putExtra("toHour", toHour);
			i.putExtra("toMinute", toMin);
		}
		startService(i);
	}

	// Dừng chạy service
	public void stopService() {
		stopService(new Intent(this, BlockCallService.class));
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		// Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		// Toast.makeText(this, "onReStart", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
		
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		// Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
	}

	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		
		if (backPressed + 2000 > System.currentTimeMillis()) {
			// luu trang thai cua app va khoi chay service
			if (swCall.isChecked()) {
				//Toast.makeText(getApplicationContext(), "start service roi nhe :v", Toast.LENGTH_SHORT).show();
				startService();
				editor.putBoolean("switch", true);
				editor.commit();
			} else {
				//Toast.makeText(getApplicationContext(), "stop service cmm di -_-", Toast.LENGTH_SHORT).show();
				stopService();
				editor.putBoolean("switch", false);
				editor.commit();
			}
			editor.putInt("fromHour", fromHour);
			editor.putInt("fromMinute", fromMin);
			editor.putInt("toHour", toHour);
			editor.putInt("toMinute", toMin);
			if (cbAlways.isChecked()) {
				editor.putBoolean("always", true);
			} else {
				editor.putBoolean("always", false);
			}
			editor.commit();
	        super.onBackPressed();
	        return;
	    }
		Toast.makeText(getApplicationContext(), "Press again to exit", Toast.LENGTH_SHORT).show();
		backPressed = System.currentTimeMillis();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
